package com.cndw.kongfu.net;

import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.util.Map;

import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBufferInputStream;
import org.jboss.netty.buffer.ChannelBuffers;

import com.cndw.kongfu.model.ChatData;
import com.cndw.kongfu.util.TimeProcesser;
import com.cndw.kungfu.vo.ChatVO;
import com.cndw.rpg.framework.ResponseMessage;
import com.cndw.rpg.framework.codec.Compress;

import flex.messaging.io.SerializationContext;
import flex.messaging.io.amf.Amf3Input;

public class ReceiveThread implements Runnable{
	public static SerializationContext context;
	private Socket socket = null;
	private static byte bytes[] = new byte[1024];
	private static Amf3Input input = null;
	private static InputStream stream = null;
	
	public ReceiveThread(Socket socket) {
		this.socket = socket;
		context = new SerializationContext();
		context.legacyCollection = true;
		input = new Amf3Input(context);
	}

	@Override
	public void run() {
		ResponseMessage message = null;
		try {
			
			int bodySize;
			int compressSize;
			
			while (true) {
				bodySize = 0;
				compressSize = 0;
				stream = socket.getInputStream();
				ChannelBuffer buffer = ChannelBuffers.dynamicBuffer();
				do {
					int length = stream.read(bytes);
					if (length != -1)
						buffer.writeBytes(bytes, 0, length);
					while (bodySize == 0 && buffer.readableBytes() >= 4) {
						bodySize = buffer.readInt();
						if (bodySize > 1024 && compressSize == 0) {
							compressSize = bodySize;
							bodySize = 0;
						}
					}
				} while (buffer.readableBytes() < bodySize);
				ChannelBuffer bodyBuffer = null;
				if (compressSize > 0) {
					byte cpomressBytes[] = Compress.decompressBytes(buffer.readBytes(bodySize).array());
					bodyBuffer = buffer.factory().getBuffer(cpomressBytes.length);
					bodyBuffer.writeBytes(cpomressBytes);
				} else {
					bodyBuffer = buffer.factory().getBuffer(bodySize);
					bodyBuffer.writeBytes(buffer, bodySize);
				}
				input.setInputStream(new ChannelBufferInputStream(bodyBuffer));
				message = (ResponseMessage) input.readObject();
				if (message != null) {
					if (message.action.equals("chat")) {
						Map<String, Object> data = (Map<String, Object>) message.m;
						System.out.println(data.get("b") + ": " + data.get("m"));
						long id = ((Double)data.get("a")).longValue();
						String name = (String) data.get("b");
						String chat = (String) data.get("m");
						int date = TimeProcesser.getUnixTime();
						ChatData chatData = new ChatData(id, name, chat, date);
					}
					System.out.println(message.m);
				}
				buffer.clear();
				input.close();
//					MySocketRequest request2 = new MySocketRequest(1, "ActSmallController",
//							"show", cipher, new Object[] { 162 });
//					socket.getOutputStream().write(request2.serialize().array());
//					socket.getOutputStream().flush();
//					if (socket != null)
//						socket.close();
//					return message;
			}
			
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
